package com.collection.web.controller.sys;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.dubbo.config.annotation.Reference;
import com.collection.api.entity.sys.SysResource;
import com.collection.api.sys.SysResourceService;
import com.collection.web.pagination.PageRequest;
import com.collection.web.resolver.annotation.GridParam;

import net.sf.json.JSONArray;

@Controller 
@RequestMapping("/resource")
public class SysResourceController {
 
	private static Logger logger = LoggerFactory.getLogger(SysResourceController.class);
	
	@Reference(version="1.0.0")
	private SysResourceService  sysResourceService;
	 
    /**
     *	  @descrption:进入资源列表页面
     *	  @author :ZhangQiuSheng
     *	  @Date : 2017年12月27日
     */
    @RequestMapping(value="/index")
    public String list(HttpServletRequest  rquest,@GridParam PageRequest pageRequest){
    	logger.info("------资源列表加载数据-----------");
    	List<Map<String, Object>> sysResourceTreeList =  sysResourceService.findAllResource();
    	rquest.setAttribute("sysResourceTreeList", JSONArray.fromObject(sysResourceTreeList).toString());
        return "sys/resource/list";
    }
    /**
      * 进入添加页面
      * @return
      */
     @RequestMapping("/add")
     public String add(HttpServletRequest request){
    	 List<Map<String, Object>> sysResourceTreeList =  sysResourceService.findAllResource();
    	 request.setAttribute("sysResourceTreeList", JSONArray.fromObject(sysResourceTreeList).toString());
         return "sys/resource/add" ;
     }
     /**
      * 保存添加数据
      * @param resource
      * @return
      */
     @RequestMapping(value="/save", method = RequestMethod.POST )
     public Object save(SysResource sysResource,HttpServletRequest request){
     	 try {
     		  Integer id = sysResource.getId();
              sysResource.setCreateTime(new Date());
              if(id==null){
            	  sysResourceService.insert(sysResource);
              }else{
            	  sysResourceService.updateByPrimaryKey(sysResource);
              }
              List<Map<String, Object>> sysResourceTreeList =  sysResourceService.findAllResource();
         	  request.setAttribute("sysResourceTreeList", JSONArray.fromObject(sysResourceTreeList).toString());
              return  "sys/resource/list";
          } catch (Exception e) {
              logger.error("保存出错",e);
              return  "保存失败" ;
          }
     }
     /**
      * 
      * @Description :进入编辑页面
      * @date :2018年1月5日上午9:14:44
      * @version : 1.2.0
      * @param id
      * @param request
      * @return : String
      */
     @RequestMapping("/edit")
     public String edit(Integer id,HttpServletRequest request){
    	 SysResource sysResource = sysResourceService.selectByPrimaryKey(id);
    	 List<Map<String, Object>> sysResourceTreeList =  sysResourceService.findAllResource();
    	 request.setAttribute("sysResourceTreeList", JSONArray.fromObject(sysResourceTreeList).toString());
         request.setAttribute("resource", sysResource);
         return "sys/resource/edit";
     }
     
   
 	
 	/**
 	 *	  @descrption: 删除权限
 	 *	  @author :ZhangQiuSheng
 	 *	  @Date : 2017年12月27日
 	 */
 	@RequestMapping(value = "/delete", method = RequestMethod.GET)
 	public String delete(Integer id,HttpServletRequest request) {
		 int deleteByPrimaryKey = sysResourceService.deleteByPrimaryKey(id);
		 if(deleteByPrimaryKey == 1){
			 List<Map<String, Object>> sysResourceTreeList =  sysResourceService.findAllResource();
		 	 request.setAttribute("sysResourceTreeList", JSONArray.fromObject(sysResourceTreeList).toString());
			 return "sys/resource/list";
		 }
		 return "删除失败";
 	}
}
